//
//  ViewController.m
//  UITableView增删改
//
//  Created by hyl on 16/6/1.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"
#import "HYLTuangouCell.h"
#import "HYLTuangou.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *buttonView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) NSMutableArray *tuangous;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
}
#pragma mark - add,update,delete
- (IBAction)remove:(id)sender {
    NSMutableArray *ArrM=[NSMutableArray array];
    for (HYLTuangou *tuangou in self.tuangous) {
        if (tuangou.state) {
            [ArrM addObject:tuangou];
        }
        
    }
    [self.tuangous removeObjectsInArray:ArrM];
    [self.tableView reloadData];
}

- (IBAction)update:(UIButton *)sender {
    HYLTuangou *tuangou=self.tuangous[0];
    tuangou.price=@"66666";
    [self.tableView reloadData];
}

- (IBAction)add:(UIButton *)sender{
    HYLTuangou *tuangou=[[HYLTuangou alloc]init];
    tuangou.title=@"text";
    [self.tuangous insertObject:tuangou atIndex:0];
    [self.tableView reloadData];
}

#pragma mark - setter/getter
-(NSArray *) tuangous{
    if (_tuangous==nil) {
        NSMutableArray *ArrM=[NSMutableArray array];
        NSString *path=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];
        _tuangous=[NSMutableArray arrayWithContentsOfFile:path];
        for (NSDictionary *dic in _tuangous) {
            [ArrM addObject:[HYLTuangou tuangouWithDic:dic]];
        }
        _tuangous=ArrM;
    }
    return _tuangous;
    
}



#pragma mark - UITableViewDataSource
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tuangous.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HYLTuangouCell *cell=[HYLTuangouCell tuangouCellWithTableView:tableView];
    cell.tuangou=self.tuangous[indexPath.row];
    return cell;
}
#pragma mark - UITableViewDelegate
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HYLTuangou *tuangou=self.tuangous[indexPath.row];
    tuangou.state=!tuangou.state;
    [tableView reloadData];
}

@end
